package com.hjm.common.utils.yh;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class BankClient {

	private LoFunction loFunction = new LoFunction();

	
	public byte[] connServerT(byte[] reqPack,String ipAddress,int port) throws Exception {
		Socket socket = null;
		InputStream is = null;
		OutputStream os = null;
		try {
			socket = new Socket();
			socket.connect(new InetSocketAddress(ipAddress, port), 10000);
			socket.setSoTimeout(20000);
			os = socket.getOutputStream();
			is = socket.getInputStream();
			os.write(reqPack);
			os.flush();
			byte[] lenByte = new byte[2];
			is.read(lenByte, 0, 2);
			int len = loFunction.byteArrayToShort(lenByte);
			byte[] bb = new byte[len];
			is.read(bb, 0, len);
			byte[] respPack = new byte[lenByte.length + bb.length];
			System.arraycopy(lenByte, 0, respPack, 0, 2);
			System.arraycopy(bb, 0, respPack, lenByte.length, bb.length);
			return loFunction.checkByte(respPack) ? null : respPack;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			closeIO(socket, is, os);
		}
	}

	protected void closeIO(Socket socket, InputStream input, OutputStream output) {
		try {
			if (input != null) {
				input.close();
			}
			if (output != null) {
				output.close();
			}
			if (socket != null) {
				socket.close();
			}
		} catch (IOException e) {
		}
	}

	
}
